package com.wlsk.license.common.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.wlsk.license.common.utils.MoneyConverter;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @description: 分转元
 * @author: liuzhangbin
 * @date: 2025/2/18
 */
public class FenToYuanSerializer extends JsonSerializer<Long> {

    @Override
    public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (Objects.nonNull(value)) {
            BigDecimal amount = MoneyConverter.fenToYuan(value, RoundingMode.UNNECESSARY);
            jsonGenerator.writeNumber(amount.setScale(2, RoundingMode.UNNECESSARY));
        }
    }
}
